package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/*
 *  @author SHCJ
 *  @created 2025/9/29 10:44
 *  @description
 */
@SuppressWarnings("All")
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void addDept(Dept dept) {
//        数据校验
        boolean hasNull = BeanUtil.hasNullField(dept, "name", "status");
        if (!hasNull) {
            throw new RuntimeException("数据不完整");
        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);

    }

    @Override
    public PageResult<Dept> page(Integer page, Integer pageSize, String name, Integer status) {
        Integer total = deptMapper.pageTotal(name, status);
        Integer outSide = (page - 1) * pageSize;
        List<Dept> rows = deptMapper.selectByPage(outSide, pageSize, name, status);
        return new PageResult<>(total, rows);
    }

    @Override
    public Dept findById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAll();
    }
}
